<?php

namespace app\admin\controller;

use app\admin\model\AuthGroupModel;
use think\AjaxPage;
use think\Db;
use think\Loader;
use think\Config;

class AdminUser extends Admin
{
    public function index()
    {
        return $this->view->fetch();
    }

    public function indexAjax()
    {
        //查询条件
        $where = [];
        $where['uid'] = ['>', 1]; //不显示超级管理员
        $this->request->has('user_name', 'param', true) && $where['user_name'] = ['like', '%' . $this->request->param('user_name') . '%'];

        //排序字段
        $order_by = ['uid' => 'asc'];

        //查询分页数据
        $nowPage = $this->request->has('page', 'param', true) ? $this->request->param('page') : 1;
        $res = Db::name('AdminUser')->where($where)->order($order_by)->paginate(null, false, ['page' => $nowPage, 'query' => $this->request->param()]);
        $res_arr = $res->toArray();
        //$page = $res->render();
        $count = $res_arr['total']; //计算总页数
        $page = (new AjaxPage($count))->show();
        $list = $res_arr['data'];
        $allPage = intval(ceil($count / Config::get('paginate.list_rows')));

        $this->view->assign('nowPage', $nowPage); //当前页
        $this->view->assign('allPage', $allPage); //总页数
        $this->view->assign('count', $count);
        $this->view->assign('page', $page);
        $this->view->assign('list', $list);

        return ajax_return('', $this->view->fetch());
    }

    public function add()
    {
        if ($this->request->isAjax()) {

            $data = $this->request->param();

            $res = Db::name('AdminUser')->where(['user_name' => $data['user_name']])->find();
            if ($res) {
                return ajax_return_error('已存在同名用户');
            }

            if (!$data['password'] || !$data['check_password'] || $data['password'] != $data['check_password']) {
                return ajax_return_error('两次输入的密码不一致');
            }

            $data['password'] = encrypt($data['password']);
            unset($data['check_password']);

            // 验证
            $validate = Loader::validate('AdminUser');
            if (!$validate->scene('add')->check($data)) {
                return ajax_return_error($validate->getError());
            }

            $insert_id = Db::name('AdminUser')->insert($data);
            if ($insert_id > 0) {
                return ajax_return('添加成功');
            } else {
                return ajax_return_error('添加失败');
            }

        } else {
            $authGroupModel = new AuthGroupModel();
            $this->assign('ruleList', $authGroupModel->getRuleList());
            return $this->view->fetch();
        }
    }

    public function edit()
    {
        if ($this->request->isAjax()) {

            $data = $this->request->param();
            if (!$data['id']) {
                return ajax_return_error('缺少参数ID');
            } else {
                $id = $data['id'];
                unset($data['id']);
            }

            if ($data['password']) {
                if($data['password'] != $data['check_password']){
                    return ajax_return_error('两次输入的密码不一致');
                }
                $data['password'] = encrypt($data['password']);
            } else {
                unset($data['password']);
            }

            // 验证
            $validate = Loader::validate('AdminUser');
            if (!$validate->scene('edit')->check($data)) {
                return ajax_return_error($validate->getError());
            }

            $res = Db::name('AdminUser')->where(['uid' => $id])->update($data);
            if ($res > 0) {
                return ajax_return('编辑成功');
            } else {
                return ajax_return_error('编辑失败');
            }
        } else {
            $id = $this->request->param('id');
            $li = Db::name('AdminUser')->where(['uid' => $id])->find();
            unset($li['password']);

            $authGroupModel = new AuthGroupModel();
            $ruleList = $authGroupModel->getRuleList();

            $this->view->assign('id', $id);
            $this->view->assign('li', $li);
            $this->view->assign('ruleList', $ruleList);

            return $this->view->fetch();
        }
    }

    public function delete()
    {
        $ids = $this->request->param('id');
        $where['uid'] = ['in', $ids];
        $re = Db::name('AdminUser')->where($where)->delete();
        if ($re > 0) {
            return ajax_return('删除成功');
        } else {
            return ajax_return_error('删除失败');
        }
    }

}